####
# 
# File: sfxFunctions
# 
# (C) Copyright 2019. All rights reserved.
# 
# Author: ShenYongchen (shenyczz@163.com)
# 
###############################################################################
# 
if(SFX_MACRO_MESSAGE_SWITCH)
  message(STATUS "sfx runing - ${CMAKE_CURRENT_LIST_FILE}")
endif()

#[==[
  @note
#--------------------------------------------------------------------------]==]

#[==[ sfx_find_modules(<output> [<directory>...])
  @ingroup module
  @brief Find `vtk.module` files in a set of directories

  ~~~
  sfx_find_modules(<output> [<directory>...])
  ~~~

  This scans the given directories recursively for `vtk.module` files and put the
  paths into the output variable. Note that module files are assumed to live next
  to the `CMakeLists.txt` file which will build the module.
#--------------------------------------------------------------------------]==]
function (sfx_find_modules output)
  set(_all_modules)
  foreach (_module_directory IN LISTS ARGN)
    file(GLOB_RECURSE _modules "${_module_directory}/sfx.module")
    list(APPEND _all_modules ${_modules})
  endforeach ()
  set("${output}" ${_all_modules} PARENT_SCOPE)
endfunction ()

#[==[ sfx_find_kits(<output> [<directory>...])
  @ingroup module
  @brief Find `sfx.kit` files in a set of directories

  ~~~
  sfx_find_kits(<output> [<directory>...])
  ~~~

  This scans the given directories recursively for `vtk.kit` files and put the
  paths into the output variable.
#--------------------------------------------------------------------------]==]
function (sfx_find_kits output)
  set(_all_kits)
  foreach (_kit_directory IN LISTS ARGN) # ? LISTS ARGN
    file(GLOB_RECURSE _kits "${_kit_directory}/sfx.kit")
    list(APPEND _all_kits ${_kits})
  endforeach ()
  set("${output}" ${_all_kits} PARENT_SCOPE)
endfunction ()



#[==[ sfx_include(<module> [SYSTEM][PUBLIC <directory>...][PRIVATE <directory>...][INTERFACE <directory>...])
  @ingroup module
  @brief Add include directories to a module

  A wrapper around `add_dependencies` that works for modules.

  ~~~
  sfx_include(<module>
    [SYSTEM]
    [PUBLIC     <directory>...]
    [PRIVATE    <directory>...]
    [INTERFACE  <directory>...])
  ~~~

#--------------------------------------------------------------------------]==]
function (sfx_include module)
  cmake_parse_arguments(_vtk_include
    "SYSTEM"
    ""
    "INTERFACE;PUBLIC;PRIVATE"
    ${ARGN})

  if (_vtk_include_UNPARSED_ARGUMENTS)
    message(FATAL_ERROR
      "Unparsed arguments for sfx_include: "
      "${_vtk_include_UNPARSED_ARGUMENTS}.")
  endif ()

  _vtk_module_real_target(_vtk_include_target "${module}")
  _vtk_module_target_function(_vtk_include)

  set(_vtk_include_system_arg)
  if (_vtk_include_SYSTEM)
    set(_vtk_include_system_arg SYSTEM)
  endif ()

  target_include_directories("${_vtk_include_target}"
    ${_vtk_include_system_arg}
    ${_vtk_include_INTERFACE_args}
    ${_vtk_include_PUBLIC_args}
    ${_vtk_include_PRIVATE_args})
endfunction ()


#[==[ sfx_real_target(<var> <module>)
  @ingroup module-internal
  @brief The real target for a module

  ~~~
  sfx_real_target(<var> <module>)
  ~~~

  Sometimes the actual, core target for a module is required (e.g., setting
  CMake-level target properties or install rules). This function returns the real
  target for a module.
#--------------------------------------------------------------------------]==]

function(sfx_real_target var module)
  if(ARGN)
    message(STATUS "ARGN = " ${ARGN})
  endif()
  message(STATUS "ARGN = " ${ARGN})
endfunction()


#[==[
#--------------------------------------------------------------------------]==]
function (_vtk_module_real_target var module)
  if (ARGN)
    message(FATAL_ERROR
      "Unparsed arguments for _vtk_module_real_target: ${ARGN}.")
  endif ()

  set(_vtk_real_target_res "")
  if (TARGET "${module}")
    get_property(_vtk_real_target_imported
      TARGET    "${module}"
      PROPERTY  IMPORTED)
    if (_vtk_real_target_imported)
      set(_vtk_real_target_res "${module}")
    endif ()
  endif ()

  if (NOT _vtk_real_target_res)
    get_property(_vtk_real_target_res GLOBAL
      PROPERTY "_vtk_module_${module}_target_name")
    # Querying during the build.
    if (DEFINED _vtk_build_BUILD_WITH_KITS AND _vtk_build_BUILD_WITH_KITS)
      get_property(_vtk_real_target_kit GLOBAL
        PROPERTY "_vtk_module_${module}_kit")
      if (_vtk_real_target_kit)
        set(_vtk_real_target_res "${_vtk_real_target_res}-objects")
      endif ()
    # A query for after the module is built.
    elseif (TARGET "${_vtk_real_target_res}-objects")
      set(_vtk_real_target_res "${_vtk_real_target_res}-objects")
    endif ()
  endif ()

  if (NOT _vtk_real_target_res)
    set(_vtk_real_target_msg "")
    if (NOT TARGET "${module}")
      if (DEFINED _vtk_build_module)
        set(_vtk_real_target_msg
          " Is a module dependency missing?")
      else ()
        set(_vtk_real_target_msg
          " Is a `find_package` missing a required component?")
      endif ()
    endif ()
    message(FATAL_ERROR
      "Failed to determine the real target for the `${module}` "
      "module.${_vtk_real_target_msg}")
  endif ()

  set("${var}"
    "${_vtk_real_target_res}"
    PARENT_SCOPE)
endfunction ()


# public function
function(sfx_print)
  _sfx_test(CS)
  # message(STATUS "sfx_print results = " ${CS})
endfunction()

# private functions
function(_sfx_test output)
  FILE(GLOB_RECURSE _src_files "*.c" "*.cpp")
  set("${output}" ${_src_files} PARENT_SCOPE)
endfunction()
